<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$checkboxMember = $_POST['membernames'];
$checkboxMember2 = $_POST['membernames2'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	for ($i=0; $i<sizeof($checkboxMember); $i++){
  $insertSQL = sprintf("INSERT INTO Attendance (`Date`,`Class`, Class_Time, Member_Name,Not_Listed_Name) VALUES (%s, %s, %s, '".$checkboxMember[$i]."', %s)",
                       GetSQLValueString($_POST['datepicker'], "date"),
                       GetSQLValueString($_POST['classtyperadio'], "text"),
                       GetSQLValueString($_POST['classtimesradio'], "text"),
                       GetSQLValueString($_POST['namenotlisted'], "text"));

  mysql_select_db($database_CrossFit, $CrossFit);
  $Result1 = mysql_query($insertSQL, $CrossFit) or die(mysql_error());
	}
	for ($i=0; $i<sizeof($checkboxMember2); $i++){
  $insertSQL = sprintf("INSERT INTO Attendance (`Date`,`Class`, Class_Time, Member_Name,Not_Listed_Name) VALUES (%s, %s, %s, '".$checkboxMember2[$i]."', %s)",
                       GetSQLValueString($_POST['datepicker'], "date"),
                       GetSQLValueString($_POST['classtyperadio'], "text"),
                       GetSQLValueString($_POST['classtimesradio'], "text"),
                       GetSQLValueString($_POST['namenotlisted'], "text"));

  mysql_select_db($database_CrossFit, $CrossFit);
  $Result1 = mysql_query($insertSQL, $CrossFit) or die(mysql_error());
  
  $insertGoTo = "http://development.crossfit26.com/app/admin/add-attendance/";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
	}
}
